package com.summerbird.mallchat.common.user.consumer;

import com.summerbird.mallchat.common.common.constant.MQConstant;
import com.summerbird.mallchat.common.common.domain.dto.PushMessageDTO;
import com.summerbird.mallchat.common.websocket.service.WebSocketService;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.sun.deploy.security.CertStore.ALL;
import static com.sun.deploy.security.CertStore.USER;

@RocketMQMessageListener(topic = MQConstant.PUSH_TOPIC, consumerGroup = MQConstant.PUSH_GROUP, messageModel = MessageModel.BROADCASTING)
@Component
public class PushConsumer implements RocketMQListener<PushMessageDTO> {

    @Autowired
    private WebSocketService webSocketService;

    @Override
    public void onMessage(PushMessageDTO message) {
        Integer type = message.getPushType();
        switch (type){
            case USER:
                message.getUidList().forEach(uid -> {
                    webSocketService.sendToUid(message.getWsBaseMsg(), uid);
                });
                break;
            case ALL:
                webSocketService.sendMsgToAll(message.getWsBaseMsg(), null);
        }
    }
}
